package cn.bs.base;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

/*
 * 2014-11-29 <br/>
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext.xml", "classpath*:/applicationContext-mvc.xml"})
@WebAppConfiguration
//声明一个事务管理 每个单元测试都进行事务回滚 无论成功与否
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
@Transactional
public abstract class BaseCase {
    @Resource
    protected WebApplicationContext wac;
    @Resource
    protected ApplicationContext applicationContext;
    protected MockMvc mockMvc;
    protected MockHttpSession session;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)/*.addFilter(new BaseFilter(), "*//*")*/.build();
        session = new MockHttpSession();
        session.setAttribute("userId", 1L);
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("");
        System.out.println("============ test end... ============");
        System.out.println("");
    }
}
